/**
 * 
 */
package cn.osworks.aos.builder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.osworks.aos.builder.db.DbTableInfo;
import cn.osworks.aos.builder.resources.BuilderResources;
import cn.osworks.aos.core.asset.AOSUtils;
import cn.osworks.aos.core.typewrap.Dto;
import cn.osworks.aos.core.typewrap.Dtos;
import cn.osworks.aos.core.velocity.VelocityHelper;

/**
 * @author penghe
 *
 */
public class VCBuilder {

	private static Log log = LogFactory.getLog(VCBuilder.class);

	public static void build(Connection connection, Dto inDto) throws SQLException {
		String tablesString = AOSUtils.trimAll(inDto.getString("tables"));
		String[] tables = tablesString.split(",");
		for (String tableName : tables) {
			DbTableInfo dbtableinfo = new DbTableInfo();
			inDto.put("table_name", tableName);
			String name = StringUtils.lowerCase(tableName);
			inDto.put("classname",name);    
			inDto.put("upname", StringUtils.capitalize(name)); 
			ArrayList<Dto> tableList = dbtableinfo.getTabelInfoByBuilder_code(connection, inDto);
			if(AOSUtils.isEmpty(tableList)){
				log.error("生成出错，请检查配置表是否存在于表builder_code，自动生成终止。");
				return ;
			}
			Dto table_head = dbtableinfo.getTabelInfoByBuilder_Table(connection, inDto);
			inDto.put("tableList", tableList);
			inDto.put("table_head", table_head);
			String outPath =inDto.getString("outPath");
//			inDto.put("outPath", outPath+"modules/controller/");
//			VCBuilder.buildController(inDto);
//			inDto.put("outPath", outPath+"modules/service/");
//			VCBuilder.bulidService(inDto);
		VCBuilder.bulidJSP(inDto);
			//inDto.put("outPath", outPath+"modules/dao/");
			//VCBuilder.bulidSqlMapper(inDto);
		}

	}
	
	public static void buildController(Dto inDto) {
		String packageString = inDto.getString("package") ;
		Dto vmDto = Dtos.newDto();
		vmDto.put("package", packageString);
		vmDto.put("daopackage", inDto.getString("daopackage"));
		vmDto.put("servicepackage", inDto.getString("servicepackage"));
		vmDto.put("controllername", inDto.getString("controllername"));
		vmDto.put("upname", inDto.getString("upname"));
		vmDto.put("classname", inDto.getString("classname"));
		vmDto.put("author", inDto.getString("author"));
		vmDto.put("sysdate", AOSUtils.getDateTimeStr());
		vmDto.put("tablelist", inDto.get("tableList"));
		vmDto.put("table_head", inDto.get("table_head"));
		vmDto.put("jsp_path", inDto.get("jsp_path"));
		StringWriter writer = VelocityHelper.mergeFileTemplate(BuilderResources.CONTROL_JAVA_VM, vmDto);
		try {
			String outPath = inDto.getString("outPath") + "//";
			FileUtils.forceMkdir(new File(outPath));
			FileOutputStream out = new FileOutputStream(outPath + inDto.getString("upname") + "Controller.java");
			out.write(writer.toString().getBytes());
			out.close();
			if (log.isInfoEnabled()) {
				log.info("Controller文件[" + packageString + "." + inDto.getString("upname") + "Controller.java]生成成功。");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void bulidService(Dto inDto) {
		String packageString = inDto.getString("package");
		Dto vmDto = Dtos.newDto();
		vmDto.put("package", packageString);
		vmDto.put("daopackage", inDto.getString("daopackage"));
		vmDto.put("servicepackage", inDto.getString("servicepackage"));
		vmDto.put("controllername", inDto.getString("controllername"));
		vmDto.put("upname", inDto.getString("upname"));
		vmDto.put("classname", inDto.getString("classname"));
		vmDto.put("author", inDto.getString("author"));
		vmDto.put("sysdate", AOSUtils.getDateTimeStr());
		vmDto.put("tablelist", inDto.get("tableList"));
		vmDto.put("table_head", inDto.get("table_head"));
		StringWriter writer = VelocityHelper.mergeFileTemplate(BuilderResources.SERVICE_JAVA_VM, vmDto);
		try {
			String outPath = inDto.getString("outPath") + "//";
			FileUtils.forceMkdir(new File(outPath));
			FileOutputStream out = new FileOutputStream(outPath + inDto.getString("upname") + "Service.java");
			out.write(writer.toString().getBytes());
			out.close();
			if (log.isInfoEnabled()) {
				log.info("Service文件[" + packageString + "." + inDto.getString("upname") + "Service.java]生成成功。");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void bulidJSP(Dto inDto) {
		String packageString = inDto.getString("package") + ".service";
		Dto vmDto = Dtos.newDto();
		vmDto.put("upname", inDto.getString("upname"));
		vmDto.put("controllername", inDto.getString("controllername"));
		vmDto.put("classname", inDto.getString("classname"));
		vmDto.put("author", inDto.getString("author"));
		vmDto.put("sysdate", AOSUtils.getDateTimeStr());
		vmDto.put("tablelist", inDto.get("tableList"));
		vmDto.put("table_head", inDto.get("table_head"));
		vmDto.put("outControlPath", inDto.get("outControlPath"));
		
		StringWriter writer = VelocityHelper.mergeFileTemplate(BuilderResources.JSP_JAVA_VM, vmDto);
		try {
			String outJSPPath = inDto.getString("outJSPPath") + "";
			FileUtils.forceMkdir(new File(outJSPPath));
			FileOutputStream out = new FileOutputStream(outJSPPath +"/"+ inDto.getString("classname") + ".jsp");
			out.write(writer.toString().getBytes());
			out.close();
			if (log.isInfoEnabled()) {
				log.info("JSP文件[" + packageString + "." + inDto.getString("upname") + ".jsp]生成成功。");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void bulidSqlMapper(Dto inDto) {
		String packageString = inDto.getString("package");
		Dto vmDto = Dtos.newDto();
		vmDto.put("package", packageString);
		vmDto.put("daopackage", inDto.getString("daopackage"));
		vmDto.put("servicepackage", inDto.getString("servicepackage"));
		vmDto.put("controllername", inDto.getString("controllername"));
		vmDto.put("upname", inDto.getString("upname"));
		vmDto.put("classname", inDto.getString("classname"));
		vmDto.put("author", inDto.getString("author"));
		vmDto.put("sysdate", AOSUtils.getDateTimeStr());
		vmDto.put("tablelist", inDto.get("tableList"));
		vmDto.put("table_head", inDto.get("table_head"));
		StringWriter writer = VelocityHelper.mergeFileTemplate(BuilderResources.SQL_XML_VM, vmDto);
		try {
			String outPath = inDto.getString("outPath") + "//";
			FileUtils.forceMkdir(new File(outPath));
			FileOutputStream out = new FileOutputStream(outPath + inDto.getString("upname") + "Mapper.xml");
			out.write(writer.toString().getBytes());
			out.close();
			if (log.isInfoEnabled()) {
				log.info("SqlMapper文件[" + packageString + "." + inDto.getString("upname") + "SqlMapper.xml]生成成功。");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
